public class Library {
    private String name;
    private Book[] books = new Book[100];//装书
    private User[] users = new User[100];//用户数组
    private int index;
    private int position;


    private BorrowBook[] borrowBooks = new BorrowBook[100];
    private int i;


    public BorrowBook[] getBorrowBooks() {
        return borrowBooks;
    }

    public Library(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Book[] getBooks() {
        return books;
    }

    //业务
    public User login(String account, String passWord) {
        for (User user : users) {
            if (user != null) {
                if (user.getAccount().equals(account) && user.getPassWord().equals(passWord)) {
                    return user;
                }
            }
        }
        return null;
    }

    //借书
    public boolean borrowBook(String name, int count, int day, User user) {
        /*
            1、判断书是否再里面
            2、判断库存够不够
         */
        for (Book book : books) {
            if (book != null) {
                if (book.getName().equals(name)) {
                    if (count > book.getStock()) {
                        break;
                    } else {
                        // 减去借的数量
                        book.setStock(book.getStock() - count);
                        // 把书存起来，封装一个借书对象
                        BorrowBook borrowBook = new BorrowBook(user, name, count, day);
                        if (i == borrowBooks.length) {
                            return false;
                        }
                        borrowBooks[i++] = borrowBook;// 把借书记录放到数组里面
                        return true;

                    }
                }
            }

        }
        return false;
    }

    // 还书
    public boolean returnBook(String name, int count) {

        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].getName().equals(name)) {
                    System.out.println(books[i].getStock());
                    books[i].setStock(books[i].getStock() + count);
                    System.out.println(books[i].getStock());
                    return true;
                }
            }
        }
        return false;

    }

    // 加书
    public void addBook(Book book) {
        if (book == null) {// 日常判空
            return;
        }
        if (index == books.length) {
            return;
        }
        books[index++] = book;
    }

    // 注册
    public void addUser(User user) {
        if (user == null) {
            return;
        }
        if (position == users.length) {
            return;
        }
        users[position++] = user;
    }

    // 支付逻辑
    public boolean pay(User loginUser) {
        float totalMoney = 0.0f;
        for (BorrowBook borrowBook : borrowBooks) {
            if (borrowBook != null) {
                for (Book book : books) {
                    if (book != null) {
                        if (book.getName().equals(borrowBook.getName())) {
                            totalMoney += borrowBook.getCount() * borrowBook.getDay() * book.getPrice();// 总支付的钱

                        }
                    }

                }
            } else {
                break;
            }


        }

        System.out.println(totalMoney);
        // 扣钱
        if (loginUser.getBalance() >= totalMoney) {
            loginUser.setBalance(loginUser.getBalance() - totalMoney);
            //borrowBooks {借书记录，借书记录}
            clearBorrowBooks();// 清空借书记录

            return true;
        } else {
            return false;
        }

    }

    // 清空
    public void clearBorrowBooks() {
        // 要考虑清空哪个人
        for (int j = 0; j < borrowBooks.length; j++) {
            if (borrowBooks[j] != null) {
                borrowBooks[j] = null;
            }
        }
    }
}
